package com.sgb.contract.view.ui.fragment;

import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.base.viewmodel.BaseViewModel;
import com.sgb.contract.model.ContractDetailBean;
import com.sgb.contract.utils.StringUtils;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.List;

import androidx.databinding.ObservableField;

/**
 * BasicInfoViewModel
 * @author cyj
 */
public class BasicInfoViewModel extends BaseViewModel {

    public ObservableField<ContractDetailBean> bean = new ObservableField<>();
    /** 发起方名称 */
    public ObservableField<String> takerName = new ObservableField<>("");
    /** 甲方签署时间 */
    public ObservableField<String> firstPartySignTime = new ObservableField<>("");
    /** 其他方签署时间 */
    public ObservableField<String> otherSignTime = new ObservableField<>("");

    /**
     * 获取合同详情
     * @param contractId    合同id
     */
    public void getContractDetail(String contractId) {
        showLoading();
        ContractNet.getInstance().getContractApi().getContractDetail(contractId).enqueue(new AbstractCallBack<BaseEntity<ContractDetailBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractDetailBean> data) {
                bean.set(data.getData());
                setInvolves(data.getData().involves);
            }
        });
    }

    /**
     * 设置甲方签署时间
     */
    private void setInvolves(List<ContractDetailBean.InvolvesInfo> dataList) {
        for(ContractDetailBean.InvolvesInfo info : dataList) {
            if(1 == info.isTaker) {
                takerName.set(info.companyName);
            }
            if(1 == info.sort) {
                // 甲方
                firstPartySignTime.set(StringUtils.replaceSeparator(info.signTime));
            } else {
                // 其他方
                otherSignTime.set(StringUtils.replaceSeparator(info.signTime));
            }
        }
    }
}
